/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "frmContactRequests.h"
#include "ui_frmContactRequests.h"
#include <QMessageBox>
QMap<QString,QListWidgetItem*> stringLinkRequest;
QMap<QListWidgetItem*,QString> requestLinkString;
QStringList friendRequests;
QObject *formFR;

frmContactRequests::frmContactRequests(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmContactRequests)
{
    setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
}

void frmContactRequests::setForm(frmLogin* login)
{
    formFR = login;
}

void frmContactRequests::setContacts(QStringList contacts)
{
    ui->lwRequests->clear();
    friendRequests = contacts;
    foreach(const QString& contact, contacts)
    {
        QListWidgetItem *contactItem = new QListWidgetItem(contact);
        contactItem->setIcon(QIcon(":/gui/KeppeMessengerAvatar.png"));
        stringLinkRequest[contact] = contactItem;
        requestLinkString[contactItem] = contact;
        ui->lwRequests->addItem(contactItem);
    }
    if (contacts.length() == 1)
    {
        ui->labFR->setText("You have 1 Friend Request");
    }
    else
    {
        ui->labFR->setText("You have " + QString::number(contacts.length()) + " Friend Requests");
    }
}

void frmContactRequests::updateNumber(QStringList contacts)
{
    if (contacts.length() == 1)
    {
        ui->labFR->setText("You have 1 Friend Request");
    }
    else
    {
        ui->labFR->setText("You have " + QString::number(contacts.length()) + " Friend Requests");
    }
}

frmContactRequests::~frmContactRequests()
{
    delete ui;
}

void frmContactRequests::on_cmdClose_clicked()
{
    this->close();
}

void frmContactRequests::on_cmdAccept_clicked()
{
    QListWidgetItem *item = ui->lwRequests->currentItem();
    if (requestLinkString.contains(item))
    {
        frmLogin *login = (frmLogin*)formFR;
        int ret = login->addUserContact(requestLinkString[item]);
        if (ret == 200)
        {
            login->updateFriendRequestList("",requestLinkString[item]);
            login->addContactItem(requestLinkString[item]);
            friendRequests.removeAll(requestLinkString[item]);
            stringLinkRequest.remove(requestLinkString[item]);
            requestLinkString.remove(item);
            updateNumber(friendRequests);
            delete item;
        }
        else
        {
            QMessageBox::warning(this,"Keppe Messenger","Accept Friend Request failed!");
        }
    }
}

void frmContactRequests::on_cmdReject_clicked()
{
    QListWidgetItem *item = ui->lwRequests->currentItem();
    if (requestLinkString.contains(item))
    {
        frmLogin *login = (frmLogin*)formFR;
        int ret = login->delUserContact(requestLinkString[item]);
        if (ret == 200)
        {
            login->updateFriendRequestList("",requestLinkString[item]);
            friendRequests.removeAll(requestLinkString[item]);
            stringLinkRequest.remove(requestLinkString[item]);
            requestLinkString.remove(item);
            updateNumber(friendRequests);
            delete item;
        }
        else
        {
            QMessageBox::warning(this,"Keppe Messenger","Reject Friend Request failed!");
        }
    }
}
